using FServer.API.Extensions;
using FServer.Infrastructure.Extensions;
using FServer.Application.Extensions;
using Serilog;

var builder = WebApplication.CreateBuilder(args);

// 配置 Serilog
builder.Host.UseSerilog((context, configuration) =>
{
    configuration
        .ReadFrom.Configuration(context.Configuration)
        .Enrich.FromLogContext()
        .Enrich.WithProperty("Application", "FServer")
        .Enrich.WithProperty("Environment", context.HostingEnvironment.EnvironmentName)
        .WriteTo.Console()
        .WriteTo.File("logs/fserver-.log", rollingInterval: Serilog.RollingInterval.Day)
        .WriteTo.Elasticsearch(new Serilog.Sinks.Elasticsearch.ElasticsearchSinkOptions(new Uri("http://localhost:9200"))
        {
            IndexFormat = "fserver-{0:yyyy.MM.dd}",
            AutoRegisterTemplate = true
        });
});

// 添加服务
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Services.AddApplicationServices();
builder.Services.AddApiServices();

var app = builder.Build();

// 配置管道
app.UseApiPipeline();

try
{
    Log.Information("🚀 启动游戏服务器...");
    await app.RunAsync();
}
catch (Exception ex)
{
    Log.Fatal(ex, "❌ 游戏服务器启动失败");
    throw;
}
finally
{
    Log.CloseAndFlush();
}
